﻿using AntDesign.ProLayout;
using ERP.Abp.AntDesignUI.Exssions;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.SimpleStateChecking;

namespace ERP.Abp.AntDesignUI.Exssions
{
    public static class NavigationManagerExssion
    {
        public static string QueryString(this NavigationManager navigationManager, string name)
        {
            return navigationManager.Uri.QueryString(name);
        }
    }
}
namespace Volo.Abp.UI.Navigation
{
    public interface IMyMenuManager : IMenuManager
    {
        Task<ApplicationMenuItem> GetMenuItem(string name);
        MenuDataItem[] GetAntMenuData(ApplicationMenuItemList applicationMenu = null);

    }
    [ExposeServices(typeof(IMyMenuManager))]
    public class MyMenuManager : MenuManager, IMyMenuManager
    {
        [Inject] NavigationManager NavigationManager { get; set; }
        static ApplicationMenu _mainMenu = null;
        static List<ApplicationMenuItem> _applicationMenuItems = null;
        static Dictionary<string, ApplicationMenuItem> _keyValues = new Dictionary<string, ApplicationMenuItem>();

        public MyMenuManager(IOptions<AbpNavigationOptions> options, IServiceScopeFactory serviceScopeFactory, ISimpleStateCheckerManager<ApplicationMenuItem> simpleStateCheckerManager) : base(options, serviceScopeFactory, simpleStateCheckerManager)
        {
        }

        public static ApplicationMenu MainMenu => _mainMenu;
        private static List<ApplicationMenuItem> ApplicationMainMenus => _applicationMenuItems;
        /*
        public MyMenuManager(IOptions<AbpNavigationOptions> options, IHybridServiceScopeFactory serviceScopeFactory, ISimpleStateCheckerManager<ApplicationMenuItem> simpleStateCheckerManager) : base(options, serviceScopeFactory, simpleStateCheckerManager)
        {

        }
        */
        public async Task<ApplicationMenuItem> GetMenuItem(string name)
        {
            if (_keyValues == null || _keyValues.Count == 0)
            {
                _keyValues = new Dictionary<string, ApplicationMenuItem>();
                var list = await GetAllMenuItems();
                list.ForEach(menu =>
                {
                    if (!_keyValues.ContainsKey(menu.Name) && !string.IsNullOrWhiteSpace(menu.Name))
                        _keyValues.Add(menu.Name, menu);
                    else
                    {
                        string msg = $"菜单重复请检查【{menu.Name}{menu.DisplayName}】";
                        Console.WriteLine(msg);
                    }
                });
            }
            return _keyValues[name];
        }
        public async Task<List<ApplicationMenuItem>> GetAllMenuItems(ApplicationMenuItemList applicationMenu = null)
        {
            if (applicationMenu == null)
            {
                _applicationMenuItems = new List<ApplicationMenuItem>();
                _mainMenu = await GetMainMenuAsync();
                applicationMenu = _mainMenu.Items;
            }
            applicationMenu.ForEach(async menu =>
            {
                if (!_applicationMenuItems.Exists(m => m.Name == menu.Name) && !string.IsNullOrWhiteSpace(menu.Name))
                    _applicationMenuItems.Add(menu);
                else
                    Console.WriteLine($"GetAllMenuItems:{menu.Name}{menu.DisplayName}菜单被过滤了，原因重复name了");
                if (menu.Items != null && menu.Items.Count > 0)
                {
                    await GetAllMenuItems(menu.Items);
                }
            });

            return _applicationMenuItems;
        }
        public MenuDataItem[] GetAntMenuData(ApplicationMenuItemList applicationMenu = null)
        {

            if (_mainMenu == null)
            {

                _mainMenu = GetMainMenuAsync().Result;
            }
            if (applicationMenu == null) { applicationMenu = _mainMenu.Items; }
            List<MenuDataItem> l = new List<MenuDataItem>();
            applicationMenu.Select(m =>
            {
                var url = m.Url?.Replace("~", "").TrimEnd('/');
                url = string.IsNullOrWhiteSpace(url) ? "/" : url;
                string path = "";
                if (url.Contains("?") && url.Contains("="))
                { //带查询参数
                    path = url + $"&{Const.NavigationQueryKey}=" + m.Name;
                }
                else if (!url.Contains("?"))
                {
                    path = url + $"?&{Const.NavigationQueryKey}=" + m.Name;
                }
                var a = new MenuDataItem
                {
                    Path = path,
                    Name = m.DisplayName,
                    Key = m.Name,
                    Icon = m.Icon,
                };
                if (m.Items != null && m.Items.Count > 0)
                {
                    a.Children = GetAntMenuData(m.Items);
                }
                else
                {
                    a.Children = null;
                }
                if (!l.Exists(M => M.Key == a.Key) && !string.IsNullOrWhiteSpace(a.Key))
                    l.Add(a);
                return a;
            }).ToArray();
            return l.ToArray();
        }
    }
}
